import 'package:dio/dio.dart';

import '../api/api.dart';
import '../common/http_manager.dart';
import '../common/user_info_model.dart';
import '../model/article_model.dart';
import '../model/banner_model.dart';
import '../model/base_model.dart';
import '../model/system_tree_content_model.dart';
import '../model/system_tree_model.dart';
import '../model/user_model.dart';

class ApiService {

  // 注册
  void register(String username, String password, Function callback, Function failCallback) async {
    FormData formData = FormData.fromMap({
      "username": username,
      "password": password,
      "repassword": password
    });
    HttpManager().post(Api.USER_REGISTER, formData).then((response) {
      //print(response.toString());
      BaseModel model = BaseModel.fromJson(response.data);
      if (model.errorCode == 0) {
        var userModel = UserModel.fromJson(model.data);
        List<String>? cookies = response.headers["set-cookie"];
        User().saveUserInfo(userModel, cookies);
        callback(userModel);
      } else {
        failCallback(model.errorMsg);
      }
    });
  }

  // 登录
  void login(String username, String password, Function callback, Function failCallback) async {
    FormData formData = FormData.fromMap({"username": username, "password": password});
    HttpManager().post(Api.USER_LOGIN, formData).then((response) {
      //logger.d(response.data);
      BaseModel model = BaseModel.fromJson(response.data);
      if (model.errorCode == 0) {
        var userModel = UserModel.fromJson(model.data);
        List<String>? cookies = response.headers["set-cookie"];
        User().saveUserInfo(userModel, cookies);
        callback(userModel);
      } else {
        failCallback(model.errorMsg);
      }
    });
  }

  void getBanner(Function callback) async {
    HttpManager().get(Api.HOME_BANNER).then((response) {
      BaseModel model = BaseModel.fromJson(response.data);
      if (model.errorCode == 0) {
        var list = (model.data as List<dynamic>)
            .map((e) => BannerModel.fromJson(e as Map<String, dynamic>))
            .toList();
        callback(list);
      }
    });
  }

  void getArticleList(int page, Function callback, Function errorCallback) async {
    HttpManager().get("${Api.HOME_ARTICLE_LIST}$page/json").then((response) {
      BaseModel model = BaseModel.fromJson(response.data);
      if (model.errorCode == 0) {
        var data = model.data as Map<String, dynamic>;
        var list = (data["datas"] as List<dynamic>)
            .map((e) => ArticleModel.fromJson(e as Map<String, dynamic>))
            .toList();
        callback(list);
      } else {
        errorCallback(model.errorMsg);
      }
    }).catchError((e) {
      errorCallback(e);
    });
  }

  // 获取知识体系列表
  void getSystemTree(Function callback, Function errorCallback) async {
    HttpManager().get(Api.SYSTEM_TREE).then((response) {
      callback(SystemTreeModel(response.data));
    }).catchError((e) {
      errorCallback(e);
    });
  }

  // 获取知识体系列表详情
  void getSystemTreeContent(int page, int cid, Function callback, Function errorCallback) async {
    HttpManager().get("${Api.SYSTEM_TREE_CONTENT}$page/json?cid=$cid").then((response) {
      callback(SystemTreeContentModel(response.data));
    }).catchError((e) {
      errorCallback(e);
    });
  }

}
